import * as types from './constans'
import { reqSongDetail, reqLyric, reqHotComment } from '@/api/song'

import { parseLyric } from '@/utils/parse-lyric'
import { savePlayListId, saveCurSongIdx } from '@/utils/localstorage'
import { getRan } from '../../../utils/math-utils'

// 歌曲详情Action




export const changeCurSong = (curSong) => ({
    type: types.CHANGE_CUR_SONG,
    curSong
})
export const changeCurrentSongAction = changeCurSong

// 更改歌曲索引Action

export const changeCurSongIdx = (i) => {
    console.log(i);
    saveCurSongIdx(i)
    return ({
        type: types.CHANGE_CUR_SONG_IDX,
        i
    })
}
export const changeSongIndexAction = changeCurSongIdx
    // 更改播放列表Action


export const changePlayList = (playList) => ({
    type: types.CHANGE_PLAY_LIST,
    playList
})
export const changePlayListAction = changePlayList
    // 改变歌词Action

export const changeLyrList = (lyrList) => ({
    type: types.CHANGE_LYR_LIST,
    lyrList
})
export const changeLyricAction = changeLyrList


// 改变热门评论Action

export const changeHotComment = (hotComments) => ({
    type: types.CHANGE_HOT_COMMENT,
    hotComments
})

// 改变歌曲数量
const changePlayListCount = (count) => ({
    type: types.CHANGE_PLAY_LIST_COUNT,
    count,
})

// 首次加载Action
export const changeFirstLoad = (isFirstLoad) => ({
    type: types.CHANGE_FIRST_LOAD,
    isLoad: isFirstLoad,
})

// 改变currentLyricIndex
export const changeCurLyrIdx = (i) => ({
    type: types.CHANGE_CUR_LYR_IDX,
    i
})
export const changeCurrentLyricIndexAction = changeCurLyrIdx

// 更改播放顺序Action
export const changePlaySequence = (sequence) => ({
    type: types.CHANGE_PLAY_SEQUENCE,
    sequence
})
export const changePlaySequenceAction = changePlaySequence
    // 更改播放顺序Action
export const changeCurCommentTotal = (total) => ({
    type: types.CHANGE_CUR_COMMENT_TOTAL,
    total
})

export const changeCurrentCommentTotal = changeCurCommentTotal
    // 切换歌曲Action

export const changeCurSongAndIdx = (tag) => {
    return (dispatch, getState) => {
        const state = getState()
        const playList = state.getIn(['player', 'playList']);
        const playSequence = state.getIn(['player', 'playSequence']);
        let curSongIdx = state.getIn(['player', 'curSongIdx']);
        const len = playList.length;
        console.log(playSequence, curSongIdx, 'playSequence');
        switch (playSequence) {

            case 1:
                let random = getRan(len);
                while (random === curSongIdx) {
                    random = getRan(len)
                }
                curSongIdx = random;
                break;
            case 2:
                // 2单曲
                console.log(playSequence, ' // 2单曲');
                return
            default:
                console.log('curSongIdx + tag)', curSongIdx + tag, len);
                curSongIdx = (curSongIdx + tag) % len;
                console.log(curSongIdx);
                break
        }
        console.log(playSequence);
        const song = playList[curSongIdx];
        dispatch(changeCurSong(song));
        dispatch(changeCurSongIdx(curSongIdx));
        dispatch(getLyrList(song.id))
    }
}

export const changeCurrentIndexAndSongAction = changeCurSongAndIdx


// 修改播放列表并修改歌曲数量
export const changePlayListAndCount = (playlist) => {
    return (dispatch) => {
        dispatch(changePlayList(playlist))
        dispatch(changePlayListCount(playlist.length))
    }
}
export const changePlaylistAndCount = changePlayListAndCount


export const getSongDetail = (id) => {
        return async(dispatch, getState) => {
            const state = getState();
            const playList = state.getIn(['player', 'playList']);
            const i = playList.findIndex(v => v.id === id);
            let song = null;
            if (i !== -1) {
                song = playList[i];
                dispatch(changeCurSongIdx(i));
                console.log('歌单存在');
            } else {
                const res = await reqSongDetail(id);
                console.log(res);

                song = res.songs[0];
                if (!song) {
                    return
                }
                console.log('不存在');
                savePlayListId(id);
                playList.push(song)
                dispatch(changeCurSongIdx(playList.length - 1));
                dispatch(changePlayListAndCount(playList))
            }
            dispatch(changeCurSong(song));
            dispatch(getLyrList(id))
        }
    }
    // 歌曲详情network request





export const getSongDetailArrAction = (index) => {
    // 为什么单独抽离: (是根据listId来进行存储的)

    return async(dispatch, getState) => {
        let playList = getState().getIn(['player', 'playList'])
        if (!playList.length) {
            console.log('本地缓存mei 有数据');
            return
        } else {
            console.log('本地缓存有数据');
        }

        let song = null;
        song = playList[index]
        dispatch(changeCurSong(song))
        dispatch(changeCurSongIdx(index))
        dispatch(getLyrList(song.id))
    }
}


// 歌词network request
export const getLyrList = (id) => {
    return async(dispatch) => {
        const res = await reqLyric(id);
        let lyric = res.lrc && res.lrc.lyric;
        if (lyric) {
            lyric = parseLyric(lyric);
            dispatch(changeLyrList(lyric))
        }
    }
}
export const getLyricAction = getLyrList


export const getAddSongDetail = (id) => {
        return async(dispatch, getState) => {
            const playList = getState().getIn(['player', 'playList']);
            const i = playList.findIndex(v => v.id === id);
            if (i !== -1) {
                return
            }
            const res = await reqSongDetail(id)
            const song = res.song && res.songs[0]
            playList.push(song)
            savePlayListId(id);
            dispatch(changePlayListAndCount(playList))
        }
    }
    // 获取歌曲详情用于添加到播放列表
export const getAddSongDetailAction = getAddSongDetail

// 获取歌曲热门评论
export const getHotComment = (id) => {
    return async dispatch => {
        const res = await reqHotComment(id);
        const hotComments = res && res.hotComments;
        dispatch(changeHotComment(hotComments))
    }
}
export const getHotCommentAction = getHotComment